﻿@{
    ViewBag.Title = "CreateCarScrap";
    ViewBag.First = "车辆管理";
    ViewBag.Second = "创建车辆报废";
    Layout = "~/Views/Shared/_BaseLayout.cshtml";
}
<script src="~/Content/laydate-v1.1/laydate/laydate.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        initSingleDate("txtCarScrapTime", "YYYY/MM/DD");
        initSingleDate("txtDjTime", "YYYY/MM/DD");
    });
    //车辆选择
    function selectCar() {
        layer.open({
            type: 2,
            title: "报废车辆选择",
            shade: [0],
            area: [($(window).width() - 20) + "px", ($(window).height() - 80) + "px"],
            content: ['@Url.Action("SelectCarInfo", "CarInfo")', 'yes']
        });
    }
    //车辆选择确认
    function sureCarInfo(id, carNo) {
        var car = $("#txtCar");
        car.val(carNo); car.attr("carId", id);
        layer.closeAll();
    }
    //数据保存
    function saveData(tag) {
        var txtCar = $("#txtCar").val();
        var txtReson = $("#txtReson").val();
        var txtCarScrapTime = $("#txtCarScrapTime").val();
        var txtDjTime = $("#txtDjTime").val();
        var txtUseAge = $("#txtUseAge").val();
        var txtCzl = $("#txtCzl").val();
        var txtMoney = $("#txtMoney").val();
        var txtAddress = $("#txtAddress").val();
        if (txtCar.length <= 0) {
            Tips("请选择要报废的车辆", "txtCar");
            return false;
        }
        if (txtReson.length <= 0) {
            Tips("报废原因不能为空", "txtReson");
            return false;
        }
        if (txtCarScrapTime.length <= 0) {
            Tips("请选择报废时间", "txtCarScrapTime");
            return false;
        }
        if (txtDjTime.length <= 0) {
            Tips("请选择登记时间", "txtDjTime");
            return false;
        }
        if (txtUseAge.length <= 0) {
            Tips("使用年限不能为空", "txtUseAge");
            return false;
        }
        if (txtCzl.length <= 0) {
            Tips("请输入残值率", "txtCzl");
            return false;
        }
        if (isNaN(txtCzl)) {
            Tips("残值率只能为数字", "txtCzl");
            return false;
        }
        var ent = new Object();
        ent.CarInfoId = $("#txtCar").attr("carId");
        ent.ScrapTime = txtCarScrapTime;
        ent.RegistrationTime = txtDjTime;
        ent.ServiceLife = txtUseAge;
        ent.ScrapAddress = txtAddress;
        ent.ResidualValue = txtCzl;
        ent.ResidualAmount = txtMoney;
        ent.ScrapReson = txtReson;
        var tp = $("#tp").find("li");
        var paths = "";
        for (var i = 0; i < tp.length; i++) {
            if (i > 0) {
                paths += ",";
            }
            paths += tp[i].id;
        }
        var url = "@Url.Action("SaveCarScrap")";
        var load = loadTip("数据提交中，请稍候", 16, 0);
        $.post(url, { data: JSON.stringify(ent), fp: paths }, function (data) {
            if (data > 0) {
                if (tag == 1) {
                    layer.msg("数据保存成功", { icon: 1, time: 1000, shade: [0.3, '#000'] }, function () {
                        document.getElementById("formData").reset();
                        $("#tp").find("li").remove();
                    });
                } else {
                    layer.msg("数据保存成功", { icon: 1, time: 1000, shade: [0.3, '#000'] }, function () {
                        back();
                    });
                }
            }
        });
    }
    function back() {
        window.location.href = "@Url.Action("Index")";
    }
    //文件上传
    function fileUpload() {
        var load = loadTip('图片处理中，请稍后', 16, 0);
        $.ajaxFileUpload({
            url: "@Url.Action("FileUpload", "Base")",
            secureuri: false,
            fileElementId: "fl",
            type: 'POST',
            dataType: 'text',
            success: function (data) {
                var ul = document.getElementById("tp");
                var li = document.createElement("li");
                li.style.width = "140px";
                li.style.height = "140px";
                li.id = data;
                li.innerHTML = "<a><img width='120px'style='border-radius:5px' height='120px' src='/UploadFiles/CarImg/" + data + "' /></a><h2 style='line-height:130px;margin-left:-20px;color:red;cursor:pointer;' onclick='delPic(this);'>删除</h2>";
                ul.appendChild(li);
                layer.close(load);
            }
        });
    }
    //图片删除
    function delPic(obj) {
        layer.confirm("您确定要删除吗？", function () {
            $(obj).parent().remove();
            layer.closeAll();
        });
    }
</script>
<div class="formbody">
    <div class="formtitle"><span>创建车辆报废</span></div>
    <form method="post" enctype="multipart/form-data" class="jqtransform" id="formData" style="margin-top:-20px;">
        <table style="width:98%;margin-left:20px;line-height:48px;">
            <tr>
                <td><label>报废车辆</label></td>
                <td>
                    <input type="text" readonly="readonly" class="dfinput" id="txtCar" />
                    <input type="button" class="scbtn" onclick="selectCar();" value="选择" />
                </td>
                <td><label>报废原因</label></td>
                <td>
                    <input type="text"  class="dfinput" id="txtReson" />
                </td>
            </tr>
            <tr>
                <td><label>报废时间</label></td>
                <td><input type="text" class="dfinput" readonly="readonly" id="txtCarScrapTime" /></td>
                <td><label>登记时间</label></td>
                <td><input type="text" class="dfinput" readonly="readonly" id="txtDjTime" /></td>
            </tr>
            <tr>
                <td><label>使用年限</label></td>
                <td><input type="text" class="dfinput" id="txtUseAge" /> 年</td>
                <td><label>残值率</label></td>
                <td><input type="text" class="dfinput" id="txtCzl" /> %</td>
            </tr>
            <tr>
                <td><label>残值金额</label></td>
                <td><input type="text" class="dfinput" id="txtMoney" /> 元</td>
                <td><label>报废地点</label></td>
                <td><input type="text" class="dfinput" id="txtAddress" /></td>
            </tr>
        </table>
        <div class="formtitle"><span>报废照片</span></div>
        <div class="toolsli">
            <ul class="toollist" id="tp"></ul>
            <span class="tooladd" style="cursor:pointer" onclick="fl.click();"><img src="~/Content/images/add.png" alt="添加" /></span>
        </div>
        <input type="file" onchange="fileUpload()" style="display:none" name="fl" id="fl" />
        <input type="button" class="btn" onclick="saveData(1);" value="保存继续" />
        <input type="button" class="btn" onclick="saveData(2);" value="保存退出" />
        <input type="button" class="btn" onclick="back();" value="返 回" />
    </form>
</div>

